

clear

use "$P_Data_Processed/daily_station_data_e5_7_21.dta", replace


merge m:1 StID using "$P_Data_Processed/cluster_mkts.dta"
keep if _merge==3
drop _merge

gener post_jun2017 = (year_month>689)
sort StID post_jun2017 year_month date

by StID post_jun2017: gener use_jun2017 = 1 if _n==1

gegen wh_margin_jun2017 = mean(margin_wholesale), by(StID post_jun2017)
gegen price_jun2017 = mean(AvgP), by(StID post_jun2017)


gegen mean_temp_jun2017 = mean(temperature), by(StID post_jun2017)
gegen sd_temp_jun2017 = sd(temperature), by(StID post_jun2017)

gegen mean_precip_jun2017 = mean(precipitation), by(StID post_jun2017)
gegen sd_precip_jun2017 = sd(precipitation), by(StID post_jun2017)


gegen employed_share_jun2017 = mean(employed_share), by(StID post_jun2017)
gegen med_age_jun2017 = mean(med_age), by(StID post_jun2017)
gegen l_pop_jun2017 = mean(l_pop), by(StID post_jun2017)
gegen l_gdp_jun2017 = mean(l_gdp), by(StID post_jun2017)
gegen pop_density_jun2017 = mean(pop_density), by(StID post_jun2017)


by StID post_jun2017: keep if _n==1
drop if Post==0


gener station = 1 
gegen postal_n_competitors = total(station), by(mktid post_jun2017)
gegen n_brand = total(station), by(Brand post_jun2017)

gener post_jun2017_treat = post_jun2017*treat_group

capture drop n_post_adopt
gegen n_post_adopt = total(treat_group), by(mktid post_jun2017)
replace n_post_adopt = 0 if post_jun2017==0

replace n_post_adopt = n_post_adopt - 1 if treat_group==1 & post_jun2017==1
replace n_post_adopt = 0 if treat_group==1 & postal_n_competitors==1

label variable post_jun2017_treat "Adopter $\times$ Post June 2017"

reghdfe wh_margin_jun2017 post_jun2017_treat postal_n_competitors mean_temp_jun2017 sd_temp_jun2017 mean_precip_jun2017 sd_precip_jun2017 employed_share_jun2017 med_age_jun2017 l_pop_jun2017 l_gdp_jun2017 pop_density_jun2017 n_brand , absorb(post_jun2017 StID) vce(cluster mktid)
	outreg2 using "$P_Tables/Table_E1", replace tex(frag) dec(3) label keep(post_jun2017_treat)

reghdfe price_jun2017 post_jun2017_treat postal_n_competitors mean_temp_jun2017 sd_temp_jun2017 mean_precip_jun2017 sd_precip_jun2017 employed_share_jun2017 med_age_jun2017 l_pop_jun2017 l_gdp_jun2017 pop_density_jun2017 n_brand, absorb(post_jun2017 StID) vce(cluster mktid)
	outreg2 using "$P_Tables/Table_E1", append tex(frag) dec(3) label keep(post_jun2017_treat)
	
	** generating simple IV - share of adopters in your brand
gegen n_brand_treat = total(treat_group), by(Brand post_jun2017)
replace n_brand_treat =0 if post_jun2017==0
gener share_others_treated = (n_brand_treat)/(n_brand-1) if treat_group==0
replace share_others_treated = (n_brand_treat-1)/(n_brand-1) if treat_group==1
replace share_others_treated =0 if post_jun2017==0

	
ivreghdfe wh_margin_jun2017  postal_n_competitors mean_temp_jun2017 sd_temp_jun2017 mean_precip_jun2017 sd_precip_jun2017 employed_share_jun2017 med_age_jun2017 l_pop_jun2017 l_gdp_jun2017 pop_density_jun2017 n_brand (post_jun2017_treat = share_others_treated) , absorb(post_jun2017 StID) cluster(mktid)
	outreg2 using "$P_Tables/Table_E1", append tex(frag) dec(3) label keep(post_jun2017_treat)

	ivreghdfe price_jun2017  postal_n_competitors mean_temp_jun2017 sd_temp_jun2017 mean_precip_jun2017 sd_precip_jun2017 employed_share_jun2017 med_age_jun2017 l_pop_jun2017 l_gdp_jun2017 pop_density_jun2017 n_brand (post_jun2017_treat = share_others_treated) , absorb(post_jun2017 StID) cluster(mktid)
	outreg2 using "$P_Tables/Table_E1", append tex(frag) dec(3) label keep(post_jun2017_treat)
